/*
 *  Copyright (c) 2009, Hugo Augusto Alves
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *      * Redistributions of source code must retain the above copyright
 *        notice, this list of conditions and the following disclaimer.
 *      * Redistributions in binary form must reproduce the above copyright
 *        notice, this list of conditions and the following disclaimer in the
 *        documentation and/or other materials provided with the distribution.
 *      * Neither the name of the Hugo Augusto Alves nor the
 *        names of its contributors may be used to endorse or promote products
 *        derived from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 *  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *  DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
 *  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.wiscriptor.core;

import java.awt.Color;
import java.awt.Font;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import org.ini4j.Ini;
import org.wiscriptor.io.FileLoader;
import org.wiscriptor.utils.Serializer;

/**
 *
 * @author Hugo
 */
public class Settings {

    private static Settings instance;
    private Ini setIni;
    private File file = null;
    private Serializer serializer;

    private static final String GENERAL = "general";
    private static final String LOOK_AND_FEEL = "LookAndFeel";
    private static final String LANGUAGE = "Language";
    private static final String COUNTRY = "Country";

    private static final String EDITOR = "editor";
    private static final String FONT = "Font";
    private static final String BACKGROUND = "Background";

    // general
    private String lookAndFeel;
    private String language;
    private String country;

    // editor
    private Font editorFont = new Font("Consolas", Font.PLAIN, 12);
    private Color editorBackground = new Color(200, 200, 200);

    private Settings() throws IOException, MalformedURLException, URISyntaxException {
        file = FileLoader.getInstance().loadFile("config", "settings.ini");
        setIni = new Ini(new FileInputStream(file));
        serializer = Serializer.getInstance();
    }

    public static Settings getInstance() throws IOException, MalformedURLException, URISyntaxException {
        instance = (instance == null) ? new Settings() : instance;
        return instance;
    }

    // TODO: onclose chamar aqui pra certificar que salvou todas as configuracoes?
    public void storeSettings() throws IOException {
        storeObjects();
        setIni.store(file);
    }

    private void storeObjects() {
        String string = "";
        string = serializer.serialize(editorFont);
        setIni.put(EDITOR, FONT, string);
        string = serializer.serialize(editorBackground);
        setIni.put(EDITOR, BACKGROUND, string);
        setIni.put(GENERAL, LOOK_AND_FEEL, lookAndFeel);
        setIni.put(GENERAL, LANGUAGE, language);
        setIni.put(GENERAL, COUNTRY , country);
    }

    public void loadSettings() {
        editorFont = serializer.deSerialize(setIni.get(EDITOR, FONT), Font.class);
        editorBackground = serializer.deSerialize(setIni.get(EDITOR, BACKGROUND), Color.class);
        lookAndFeel = setIni.get(EDITOR, LOOK_AND_FEEL);
        language = setIni.get(EDITOR, LANGUAGE);
        country = setIni.get(EDITOR, COUNTRY);
    }

    public Color getEditorBackground() {
        return editorBackground;
    }

    public void setEditorBackground(Color editorBackground) throws IOException {
        this.editorBackground = editorBackground;
        setIni.put(EDITOR, BACKGROUND, serializer.serialize(editorBackground));
        setIni.store(file);
    }

    public Font getEditorFont() {
        return editorFont;
    }

    public void setEditorFont(Font editorFont) throws IOException {
        this.editorFont = editorFont;
        setIni.put(EDITOR, FONT, serializer.serialize(editorFont));
        setIni.store(file);
    }

    public String getLookAndFeel() {
        return lookAndFeel;
    }

    public void setLookAndFeel(String lookAndFeel) throws IOException {
        this.lookAndFeel = lookAndFeel;
        setIni.put(GENERAL, LOOK_AND_FEEL, lookAndFeel);
        setIni.store(file);
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) throws IOException {
        this.country = country;
        setIni.put(GENERAL, COUNTRY , country);
        setIni.store(file);
    }

    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) throws IOException {
        this.language = language;
        setIni.put(GENERAL, LANGUAGE, language);
        setIni.store(file);
    }




}
